package com.lty.cloud.controller;


import com.lty.cloud.service.PaymentService;
import com.lty.cloud.entities.CommonResult;
import com.lty.cloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping(value = "/payment/create")
    public CommonResult create(Payment payment) {
        int result = paymentService.create(payment);
        log.info("insert result ->{}", result);
        if (result > 0) {
            return new CommonResult<Integer>(200, "创建成功:" + serverPort, result);
        } else {
            return new CommonResult(444, "创建失败:" + serverPort);
        }

    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") int id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果 result ->{}", payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功：" + serverPort, payment);
        } else {
            return new CommonResult(444, "查询失败：" + serverPort);
        }
    }

    @GetMapping(value = "/payment/discovery")
    public Object getDiscovery() {
        List<String> list = discoveryClient.getServices();
        list.forEach(it -> log.info("******element,{}", it));
        return null;
    }

}
